// pages/home/home.js
var WxParse = require('../wxParse/wxParse.js');

Page({
  onShareAppMessage: function (res) {
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    return {
      title: '青联暖心行动',
      path: '/pages/home/home'
    }
  },

  /**
   * 页面的初始数据
   */
  data: {
    message:[],
    heartActivity: {},
    raisePeople:'',
    raiseMoney:'',
    eventContent:'',
    bankName:'',
    bankAccount:'',
    bankId:'',
    list: [],
    hidden: true,
    meHidden: true,
    recordPage: 1,
    recordPageSize: 5,
    recordTotal: 23,
    messagePage: 1,
    messagePageSize: 5,
    messageTotal: 23,
    imgUri: 'http://tsw-qqc.oss-cn-hangzhou.aliyuncs.com',
    messageOver:false,
    freeback: ''
  },
  // 获取 暖心行动 信息
  getHeartActivity() {  
    let _this = this  
    wx.request({
      url: getApp().globalData.api + '/a/wxinterface/qinglian/queryEvent', //
      data: {
        eventId: getApp().globalData.eventId        
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        let data = res.data             
        _this.setData({
          raisePeople: data.obj.raisePeople,
          raiseMoney: data.obj.raiseMoney,
          eventContent: data.obj.eventContent
          // ,bankName: res.data.obj.tHelpPersonInfo.bankFrom,
          // bankAccount: res.data.obj.tHelpPersonInfo.bankCardName,
          // bankId: res.data.obj.tHelpPersonInfo.bankId
        })
      }
    }) 
  },
  // 获取留言 信息
  getMessage() {
    let _this = this  
    let data = []
    wx.request({
      url: getApp().globalData.api + '/a/wxinterface/qinglian/commentList', //
      data: {
        eventId: getApp().globalData.eventId,
        pageNo: _this.data.messagePage
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        let _data = res.data
        let _total = 0
        for (var i = 0; i < _data.list.length; i++) {
          data.push({
            nickname: _data.list[i].nickname,
            content: _data.list[i].content,
            headUrl: _data.list[i].headUrl
          })
          _total = _data.list[0].total
        }
        _this.setData({
          message: _this.data.message.concat(data),
          messageTotal: _total
        })
        if (_this.data.message.length >= _this.data.messageTotal) {
          _this.setData({
            messageOver:true
          })
        }
      }
    })
  },
  // 获取捐款记录
  getData(updata) {
    let data = []
    let index = this.data.recordPageSize
    let _this = this
    wx.request({
      url: getApp().globalData.api + '/a/wxinterface/event_donate', //
      data: {
        event_id: getApp().globalData.eventId,
        pageNo: _this.data.recordPage
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        let _data = res.data
        let _total = 0
        for (var i = 0; i < _data.list.length; i++) {
          data.push({
            time: _data.list[i].payTime,
            title: _data.list[i].nickname,
            donation: _data.list[i].amount,
            imgUrl: _data.list[i].headUrl
          })
          _total = _data.list[0].total
        }
        _this.setData({
          list: _this.data.list.concat(data),
          recordTotal: _total
        })
        if (_this.data.list.length >= _this.data.recordTotal) {
          _this.setData({
            hidden: false
          })
        }
      }
    })
  },
  // 上拉加载
  lower() {
    let currentPage = this.data.recordPage
    if (currentPage * this.data.recordPageSize >= this.data.recordTotal) {
      return
    }
    currentPage++
    this.setData({
      recordPage: currentPage
    });
    this.getData(true)
  },
  // 留言加载
  melower() {
    let currentPage = this.data.messagePage
    if (currentPage * this.data.messagePageSize >= this.data.messageTotal) {
      return
    }
    currentPage++
    this.setData({
      messagePage: currentPage
    });
    this.getMessage()
  },
  // 反馈信息加载
  getFeedBack() {
    let _this = this
    wx.request({
      url: getApp().globalData.api + '/a/wxinterface/feedBack', //
      data: {
        event_id: getApp().globalData.eventId
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        let data = res.data.obj.feedBack[0].feedback;  
        if (data) {
          data = data.replace(/src="/g, 'src="http://tsw-qqc.oss-cn-hangzhou.aliyuncs.com');
          WxParse.wxParse('freeback', 'html', data, _this, 5);
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onShow: function (options) {
    this.getMessage()
    this.getHeartActivity()
    this.getData()
    this.getFeedBack()
  }
})